﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace WSLuuDiemV2
{
    public class QuanLyDiem
    {
        const string FILENAME = "ketqua.txt";
        string path = "";
        public QuanLyDiem()
        {
            path = AppDomain.CurrentDomain.BaseDirectory;
        }
        public void SaveFile(List<KetQua> ds)
        {
            FileStream fs = null;            
            fs = new FileStream(path + "/" + FILENAME, FileMode.Create, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            ds.OrderByDescending(x=>x.Diem);
            foreach (var kq in ds)
            {
                sw.WriteLine(string.Format("{0}-{1}",kq.TenNguoiChoi,kq.Diem));
            }
            sw.Flush();
            sw.Close();
            fs.Close();
        }

        public List<KetQua> LoadFile()
        {
            List<KetQua> ds = null;
            if (File.Exists(path + "/" + FILENAME))
            {
                ds = new List<KetQua>();
                FileStream fs = new FileStream(path + "/" + FILENAME, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                string str = "";
                while ((str = sr.ReadLine()) != null)
                {
                    string[] kq = str.Split('-');
                    KetQua x = new KetQua() { TenNguoiChoi = kq[0], Diem = int.Parse(kq[1]) };
                    ds.Add(x);

                }
                sr.Close();
                fs.Close();
            }
            return ds;
        }
    }
}